<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑组队</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        var teamId = <%=params[:id]%>

        var workerDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/worker/getWorkersByTeam?teamId='+teamId
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'worker'
          },[
            {name:'id'},
            {name:'name'},
            {name:'number'},
            {
              name:'is_manager',
              convert: function(value){
                if(value == 1){
                  return '<font color="green">指定</font>'
                }else{
                  return '<font color="red">未指定</font>'
                }
              }
            }
          ]
        )
        });

        var workerSM = new Ext.grid.CheckboxSelectionModel();
        var workerCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          workerSM,
          {header:'姓名',sortable:true,dataIndex:'name'},
          {header:'指纹编号',sortable:true,dataIndex:'number'},
          {header:'是否指定',sortable:true,dataIndex:'is_manager'}
        ]);

        var assignButton = new Ext.Button({
          text: '指定',
          iconCls: 'addItem',
          handler: function(){
            if(workerGrid.getSelectionModel().hasSelection()){
              var rs=workerGrid.getSelectionModel().getSelections();
              var ids=[];
              var workName='';
              for(var i=0;i<rs.length;i++){
                var data=rs[i].data;
                ids.push(data.id);
                workName+=data.name+'<br>';
              }
              var keys=Ext.util.JSON.encode(ids);
              Ext.MessageBox.confirm('提示','确定指定以下人员？<br><font color="red">' + workName + '</font>',function (button){
                if(button=='yes'){
                  Ext.Ajax.request({
                    url:'/team/assign',
                    method:'post',
                    waitMsg: '正在指定人员...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        workerDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }
              });
            }else{
              Ext.Msg.alert('提示信息','请选择要指定的人员');
            }
          }
        })

        var unassignButton = new Ext.Button({
          text: '取消',
          iconCls: 'remove',
          handler: function(){
            if(workerGrid.getSelectionModel().hasSelection()){
              var rs=workerGrid.getSelectionModel().getSelections();
              var ids=[];
              var workName='';
              for(var i=0;i<rs.length;i++){
                var data=rs[i].data;
                ids.push(data.id);
                workName+=data.name+'<br>';
              }
              var keys=Ext.util.JSON.encode(ids);
              Ext.MessageBox.confirm('提示','确定取消指定以下人员？<br><font color="red">' + workName + '</font>',function (button){
                if(button=='yes'){
                  Ext.Ajax.request({
                    url:'/team/unassign',
                    method:'post',
                    waitMsg: '正在取消指定人员...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        workerDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }
              });
            }else{
              Ext.Msg.alert('提示信息','请选择要取消指定的人员');
            }
          }
        })

        var workerGrid =  new Ext.grid.GridPanel({
          ds: workerDS,
          cm: workerCM,
          sm: workerSM,
          width: 550,
          height: 250,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: workerDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar:[assignButton,unassignButton]
        });

        workerDS.load();

        var editTeamForm=new Ext.form.FormPanel({
          width:580,
          labelAlign: 'top',
          url:'/team/update',
          buttonAlign:'center',
          autoHeight:true,
          bodyStyle: 'padding:5px',
          labelWidth:100,
          frame: true,
          reader:new Ext.data.JsonReader({
          },[
            {name:'id'},
            {name:'name'},
            {name:'worker_id'}
          ]
        ),
          items:[{
              xtype:'hidden',
              name:'id',
              value:''
            },{
              layout:'column',
              items:[{
                  columnWidth:1,
                  layout:'form',
                  items:[{
                      xtype:'textfield',
                      fieldLabel: '组队名称',
                      name: 'name',
                      allowBlank:false,
                      anchor:'90%'
                    },workerGrid]
                }]
            }],
          buttons:[{
              text:'确定',
              handler: function(){
                // check form value
                if (editTeamForm.form.isValid()) {
                  editTeamForm.form.submit({
                    method: 'post',
                    waitMsg: '正在更新班组...',
                    success: function(result, response){
                      if (response.result.info == "success") {
                        //添加成功后，隐藏窗口，并刷新Grid
                        editTeamWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text:'取消',
              handler:function(){
                editTeamWin.destroy();
              }
            }]
        });
        editTeamForm.form.load({
          url: '/team/edit?id=' + teamId,
          waitMsg: '正在载入数据...',
          success: function(form, action){
          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        editTeamForm.render('editTeamForm');
      })
    </script>
  </head>
  <body>
    <div id="editTeamForm"></div>
  </body>
</html>
